$(function() {
    bgPhoto();
    $(window).resize(bgPhoto);
    
    initFBAPI();
});

function bgPhoto() {
    var img = $('#bg-photo img');
    if (! window.BgPhoto) {
        window.BgPhoto = {
            width:  img.width(),
            height: img.height()
        };
    }
    
    var win = $(window);
    var winW = win.innerWidth();
    
    img.height(window.BgPhoto.height);
    img.width(winW);
    if (window.BgPhoto.width > winW) {
        img.width(window.BgPhoto.width);
    }
    
}

function initFBAPI() {
	(
		function(d, s, id) {
			var js;
			var fjs = d.getElementsByTagName(s)[0];
			if (d.getElementById(id)) {
				return;
			}
			
			js 	   = d.createElement(s); 
			js.id  = id;
			js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&status=0";
			
			fjs.parentNode.insertBefore(js, fjs);
		} (document, 'script', 'facebook-jssdk')
	);
}

function lightbox(element, options) {
	element = $(element);
	
	options = $.extend(options, {
		dialogClass: 'lightbox',
		modal: 	     true,
		resizable:   false,
		closeOnEscapeType: true,
		open: function(ui) {
			var inpt = $('<input type="text">');
			$(ui.target).append(inpt);
			inpt.focus().hide();
		},
		beforeClose: function() {
			$(this).dialog('destroy');
		}
	});
	
	element.dialog(options)
}

function preloadImage(image, onLoading, onComplete) {
	if (image.complete) {
		onComplete(image);
	} else {
		onLoading(image);
		setTimeout(function(){
			preloadImage(image, onLoading, onComplete);
		}, 500);
	}
	
	return false;
}
